package com.xf.web.controller.sys;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xf.pm.common.PageData;
import com.xf.pm.common.PageForm;
import com.xf.pm.util.ResultMsg;
import com.xf.sys.entity.SysUser;
import com.xf.sys.service.ISysUserService;
import com.xf.web.util.SessionUtil;



/**
 *SysUserController.java
 *SysUserController
 *author:lb
 *date:2016年10月3日
 */
@Controller
@RequestMapping("/sysUser")
public class SysUserController {

	private final Logger logger =  LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private ISysUserService sysUserService;
	
	/**
	 * 初始化界面
	 * @return
	 */
	@RequestMapping(value = "/list")
	public String list()
	{
		return "sys/user/list";
	}
	
	/**
	 * 用户列表查询信息
	 * @param form
	 * @param sysUser
	 * @return
	 */
	@RequestMapping(value = "/selectSysUserList")
	public @ResponseBody PageData selectSysUserList(PageForm form,SysUser sysUser)
	{
		return sysUserService.selectSysUserList(form,sysUser);
	}
	
	/**
	 * 新增用户信息
	 * @param sysUser
	 * @return
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public @ResponseBody ResultMsg addSysUser(SysUser sysUser){
		ResultMsg msg=new ResultMsg();
        try {
        	if(null!=sysUser){
        		sysUser.setCreatorId(SessionUtil.getSession().getUserId());
        		int result=sysUserService.insertSysUser(sysUser);
        		if(result==1){
        			msg.setMsg("添加用户成功");
            		msg.setSuccess(true);
        		}else{
        			msg.setMsg("添加用户失败");
            		msg.setSuccess(false);
        		}
            }
		} catch (Exception e) {
			logger.error("添加用户结束异常");
		    e.printStackTrace();
		}
        return msg;
	}
	
	/**
	 * 删除用户信息
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "/remove", method = RequestMethod.POST)
	public @ResponseBody ResultMsg removeSysUser(Integer[] ids){
		ResultMsg msg=new ResultMsg();
        try {
        	if(null!=ids&&ids.length>0){
        		int result=sysUserService.deleteSysUser(ids);
        		if(result>0){
        			msg.setMsg("删除用户成功");
            		msg.setSuccess(true);
        		}else{
        			msg.setMsg("删除用户失败");
            		msg.setSuccess(false);
        		}
            }
		} catch (Exception e) {
			logger.error("删除用户结束异常");
		    e.printStackTrace();
		}
		return msg;
	}
	
	/**
	 * 修改用户信息
	 * @param sysUser
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody ResultMsg updateSysUser(SysUser sysUser){
		ResultMsg msg=new ResultMsg();
        try {
        	if(null!=sysUser){
        		int result=sysUserService.updateSysUser(sysUser);
        		if(result==1){
        			msg.setMsg("修改用户成功");
            		msg.setSuccess(true);
        		}else{
        			msg.setMsg("修改用户失败");
            		msg.setSuccess(false);
        		}
            }
		} catch (Exception e) {
			logger.error("修改用户结束异常");
		    e.printStackTrace();
		}
        return msg;
	}
	
	/**
	 * 查询单个用户信息
	 * @param userId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/edit")
	public String edit(Integer userId, ModelMap model)
	{
		if (userId != null)
		{
			model.addAttribute("dto", sysUserService.selectSysUser(userId));
		}
		return "sys/user/edit";
	}
	
	/**
	 * 跳转角色设置界面
	 * @param userId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/roleTree")
	public String roleTree(Integer userId,ModelMap model)
	{
		if (userId != null)
		{
			model.addAttribute("dto", sysUserService.selectSysUser(userId));
		}
		return "sys/user/roleTree";
	}
}
